<div class="tmd-doc">
<h1 class="tmd-header-1">
Expressions, Statements and Simple Statements
</h1>
<p></p>
<div class="tmd-usual">
This article will introduce expressions and statements in Go.
</div>
<p></p>
<div class="tmd-usual">
Simply speaking, an expression represents a value and a statement represents an operation. However, in fact, some special expressions may be composed of and represent several values, and some statements may be composed of several sub operations/statements. By context, some statements can be also viewed as expressions.
</div>
<p></p>
<div class="tmd-usual">
Simple statements are some special statements. In Go, some portions of all kinds of control flows must be simple statements, and some portions must be expressions. Control flows will be introduced in the next Go 101 article.
</div>
<p></p>
<div class="tmd-usual">
This article will not make accurate definitions for expressions and statements. It is hard to achieve this. This article will only list some expression and statement cases. Not all kinds of expressions and statements will be covered in this article, but all kinds of simple statements will be listed.
</div>
<p></p>
<h3 id="expressions" class="tmd-header-3">
Some Expression Cases
</h3>
<p></p>
<div class="tmd-usual">
Most expressions in Go are single-value expressions. Each of them represents one value. Other expressions represent multiple values and they are named multi-value expressions.
</div>
<p></p>
<div class="tmd-usual">
In the scope of this document, when an expression is mentioned, we mean it is a single-value expression, unless otherwise specified.
</div>
<p></p>
<div class="tmd-usual">
Value literals, variables, and named constants are all single-value expressions, also called elementary expressions.
</div>
<p></p>
<div class="tmd-usual">
Operations (without the assignment parts) using the operators introduced in the article <a href="operators.html">common operators</a> are all single-value expressions.
</div>
<p></p>
<p></p>
<div class="tmd-usual">
If a function returns at least one result, then its calls (without the assignment parts) are expressions. In particular, if a function returns more than one results, then its calls belong to multi-value expressions. Calls to functions without results are not expressions.
</div>
<p></p>
<div class="tmd-usual">
Methods can be viewed as special functions. So the aforementioned function cases also apply to methods. Methods will be explained in detail in the article <a href="method.html">method in Go</a> later.
</div>
<p></p>
<p></p>
<div class="tmd-usual">
In fact, later we will learn that custom functions, including methods, are all function values, so they are also (single-value) expressions. We will learn more about <a href="function.html">function types and values</a> later.
</div>
<p></p>
<p></p>
<div class="tmd-usual">
Channel receive operations (without the assignment parts) are also expressions. Channel operations will be explained in the article <a href="channel.html">channels in Go</a> later.
</div>
<p></p>
<p></p>
<div class="tmd-usual">
Some expressions in Go, including channel receive operations, may have optional results in Go. Such expressions can present as both single-value and multi-value expressions, depending on different contexts. We can learn such expressions in other Go 101 articles later.
</div>
<p></p>
<h3 id="simple-statements" class="tmd-header-3">
Simple Statement Cases
</h3>
<p></p>
<div class="tmd-usual">
There are six kinds of simple statements.
</div>
<p></p>
<ol class="tmd-list">
<li class="tmd-list-item">
<div class="tmd-usual">
short variable declaration forms
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
pure value assignments (not mixing with variable declarations), including <code class="tmd-code-span">x op= y</code> operations.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
function/method calls and channel receive operations. As mentioned in the last section, these simple statements can also be used as expressions.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
channel send operations.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
nothing (a.k.a., blank statements). We will learn some uses of blank statements in the next article.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
<code class="tmd-code-span">x++</code> and <code class="tmd-code-span">x--</code>.
</div>
</li>
</ol>
<p></p>
<div class="tmd-usual">
Again, channel receive and sent operations will be introduced in the article <a href="channel.html">channels in Go</a>.
</div>
<p></p>
<div class="tmd-usual">
Note, <code class="tmd-code-span">x++</code> and <code class="tmd-code-span">x--</code> can't be used as expressions. And Go doesn't support the <code class="tmd-code-span">++x</code> and <code class="tmd-code-span">--x</code> syntax forms.
</div>
<p></p>
<h3 class="tmd-header-3">
Some Non-Simple Statement Cases
</h3>
<p></p>
<div class="tmd-usual">
An incomplete non-simple statements list:
</div>
<p></p>
<ul class="tmd-list">
<li class="tmd-list-item">
<div class="tmd-usual">
standard variable declaration forms. Yes, short variable declarations are simple statements, but standard ones are not.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
named constant declarations.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
custom type declarations.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
package import declarations.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
explicit code blocks. An explicit code block starts with a <code class="tmd-code-span">{</code> and ends with a <code class="tmd-code-span">}</code>. A code block may contain many sub-statements.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
function declarations. A function declaration may contain many sub-statements.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
control flows and code execution jumps. Please read <a href="control-flows.html">the next article</a> for details.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
<code class="tmd-code-span">return</code> lines in function declarations.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
deferred function calls and goroutine creations. The two will be introduced in <a href="control-flows-more.html">the article after next</a>.
</div>
</li>
</ul>
<p></p>
<p></p>
<h3 class="tmd-header-3">
Examples of Expressions and Statements
</h3>
<p></p>
<pre class="tmd-code line-numbers">
<code class="language-go">// Some non-simple statements.
import "time"
var a = 123
const B = "Go"
type Choice bool
func f() int {
	for a &lt; 10 {
		break
	}

	// This is an explicit code block.
	{
		// ...
	}
	return 567
}

// Some simple statements:
c := make(chan bool) // channels will be explained later
a = 789
a += 5
a = f() // here f() is used as an expression
a++
a--
c &lt;- true // a channel send operation
z := &lt;-c  // a channel receive operation used as the
          // source value in an assignment statement.

// Some expressions:
123
true
B
B + " language"
a - 789
a &gt; 0 // an untyped boolean value
f     // a function value of type "func ()"

// The following ones can be used as both
// simple statements and expressions.
f()
&lt;-c // a channel receive operation
</code></pre>
<p></p>
</div>
